package org.czx.datactruct.third;

import org.czx.datactruct.first.BagInterface;

/**
 * @author czx
 * @date 2020/8/22 16:50
 */
public class LinkedBagDemo1 {
    public static void main(String[] args) {
        System.out.println("创建一个空包");
        BagInterface<String> aBag = new LinkedBag<>();
        testIsEmpty(aBag, true);
        displayBag(aBag);

        String[] contentsOfBag = {"A", "B", "D", "A", "C", "A", "D"};
        testAdd(aBag, contentsOfBag);
        testIsEmpty(aBag, false);
    }

    /**
     * 测试add方法
     * @param aBag
     * @param content
     */
    private static void testAdd(BagInterface<String> aBag, String[] content) {
        System.out.println("Adding following " + content.length + " strings to the bag:");
        for (int i = 0; i < content.length; i++) {
            if (aBag.add(content[i])) {
                System.out.print(content[i] + " ");
            } else {
                System.out.print("\nUnable to add " + content[i] + " to the bag");
            }
        }
        System.out.println();
        displayBag(aBag);
    }

    private static void displayBag(BagInterface<String> aBag) {
        System.out.println("The bag contains following strings: ");
        Object[] bagArray = aBag.toArray();
        for (int i = 0; i < bagArray.length; i++) {
            System.out.print(bagArray[i] + " ");
        }
        System.out.println();
    }

    private static void testIsEmpty(BagInterface<String> bag, boolean empty) {
        System.out.print("\nTesting isEmpty with ");
        if (empty) {
            System.out.println("an empty bag: ");
        } else {
            System.out.println("a bag that is not empty: ");
        }

        System.out.print("isEmpty finds the bag ");
        if (!empty && bag.isEmpty()) {
            System.out.println("empty, but it is not empty: ERROR.");
        } else {
            System.out.println("not empty: OK.");
        }
    }
}
